package com.cmcc.passenger.ui.profile;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;

import com.cmcc.passenger.R;
import com.cmcc.passenger.config.PassPath;
import com.cmcc.passenger.databinding.ListItemProfileMenuBinding;

public class ProfileMenuAdapter extends ListAdapter<MenuModel, ProfileMenuAdapter.MenuViewHolder>  {

    private long mLastOrderClickTime = 0;
    private long mLastAddressClickTime = 0;
    public static final long TIME_INTERVAL = 1000L;
    private ItemCliclListener listener;


   public  interface ItemCliclListener{
        void cilick(int position);

    }

    protected ProfileMenuAdapter(@NonNull DiffUtil.ItemCallback<MenuModel> diffCallback,ItemCliclListener listener) {
        super(diffCallback);
        this.listener = listener;
    }

    @NonNull
    @Override
    public MenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ListItemProfileMenuBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.list_item_profile_menu, parent, false);
        return new MenuViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuViewHolder holder, int position) {
        MenuModel menuModel = getItem(position);
        if (position == 0 || position == 5) {
            holder.binding.menuIcon.setVisibility(View.INVISIBLE);
        }
        holder.binding.menuName.setText(menuModel.getName());
        holder.binding.menuValue.setText(menuModel.getContent());
        holder.binding.getRoot().setOnClickListener(view -> {
            listener.cilick(position);
        });
    }

    static class MenuViewHolder extends RecyclerView.ViewHolder {
        public ListItemProfileMenuBinding binding;

        public MenuViewHolder(@NonNull ListItemProfileMenuBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }

    public static class SingleItemDiffCallback extends DiffUtil.ItemCallback<MenuModel> {

        @Override
        public boolean areItemsTheSame(@NonNull MenuModel oldItem, @NonNull MenuModel newItem) {
            return oldItem == newItem;
        }

        @Override
        public boolean areContentsTheSame(@NonNull MenuModel oldItem, @NonNull MenuModel newItem) {
            return oldItem.getName().equals(newItem.getName());
        }
    }

}
